package com.tianzeculture.stagebao.activity.home.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.tianzeculture.stagebao.R;
import com.tianzeculture.stagebao.activity.artist.ArtistActivity;
import com.tianzeculture.stagebao.activity.artist.MyArtistActivity;
import com.tianzeculture.stagebao.activity.authentication.BusinessAuthenticationActivity;
import com.tianzeculture.stagebao.activity.authentication.PersonalAuthenticationActivity;
import com.tianzeculture.stagebao.activity.business.BusinessHomepagerActivity;
import com.tianzeculture.stagebao.activity.mine.MyNoticeActivity;
import com.tianzeculture.stagebao.activity.mine.MyResumeActivity;
import com.tianzeculture.stagebao.activity.personal.MyBagActivity;
import com.tianzeculture.stagebao.activity.personal.MyFansActivity;
import com.tianzeculture.stagebao.activity.personal.MyFocusActivity;
import com.tianzeculture.stagebao.activity.personal.PersonalInfoActivity;
import com.tianzeculture.stagebao.activity.personal.QuestionActivity;
import com.tianzeculture.stagebao.activity.personal.RechargeActivity;
import com.tianzeculture.stagebao.activity.personal.SettingActivity;
import com.tianzeculture.stagebao.activity.personal.SuggestionActivity;
import com.tianzeculture.stagebao.base.BaseFragment;
import com.tianzeculture.stagebao.bean.ArtistsDetailedBean;
import com.tianzeculture.stagebao.bean.ResultBean;
import com.tianzeculture.stagebao.bean.User;
import com.tianzeculture.stagebao.dialog.ShareDialog;
import com.tianzeculture.stagebao.net.UserRestUsage;
import com.tianzeculture.stagebao.network.Request;
import com.tianzeculture.stagebao.network.enity.MSG;
import com.tianzeculture.stagebao.network.util.ToastUtil;
import com.tianzeculture.stagebao.util.Util;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;
import de.hdodenhof.circleimageview.CircleImageView;

import static com.tianzeculture.stagebao.net.base.AppBaseRestUsageV2.PICTURE_URL;

/**
 * Created by Administrator on 2018/5/29 0029.
 * 我的
 */

public class MyFragment extends BaseFragment implements ShareDialog.OnSelectClickListener {
    @BindView(R.id.statusView)
    View statusView;
    @BindView(R.id.ibtnSystemNews)
    ImageButton ibtnNotice;

    @BindView(R.id.imageView)
    ImageView imageView;
    @BindView(R.id.layoutTop)
    LinearLayout layoutTop;

    @BindView(R.id.ibtnFocus)
    ImageButton ibtnFocus;
    @BindView(R.id.ibtnFans)
    ImageButton ibtnFans;
    @BindView(R.id.ibtnChit)
    ImageButton ibtnChit;
    @BindView(R.id.ibtnBag)
    ImageButton ibtnBag;

    @BindView(R.id.tvFocus)
    TextView tvFocus;
    @BindView(R.id.tvFans)
    TextView tvFans;

    @BindView(R.id.tvSetting)
    TextView tvSetting;
    @BindView(R.id.tvQuestion)
    TextView tvQuestion;
    @BindView(R.id.tvSuggestion)
    TextView tvSuggestion;

    @BindView(R.id.tvMyHome)
    TextView tvMyHome;
    @BindView(R.id.tvCircular)
    TextView tvCircular;
    @BindView(R.id.tvPriceBag)
    TextView tvPriceBag;

    @BindView(R.id.iv_pi_header)
    CircleImageView ivHeader;
    @BindView(R.id.tvUserName)
    TextView tvUserName;
    @BindView(R.id.tvJob)
    TextView tvJob;
    @BindView(R.id.tvAccount)
    TextView tvAccount;
    @BindView(R.id.tvResume)
    TextView tvResume;
    @BindView(R.id.ll_title)
    LinearLayout llTitle;

    @Request
    UserRestUsage userRestUsage = null;

    private int myInfo = 101;
    ShareDialog dialog;

    @Override
    public void initUI() {
        super.initUI();

        ViewGroup.LayoutParams layoutParams = statusView.getLayoutParams();
        layoutParams.height = getStatusHeight(getContext());
        statusView.setLayoutParams(layoutParams);
        if (Util.JOB == 1) {
            tvCircular.setVisibility(View.GONE);
            tvResume.setVisibility(View.VISIBLE);
        } else {
            tvCircular.setVisibility(View.VISIBLE);
            tvResume.setVisibility(View.GONE);
        }
        if (Util.user != null) {
            userInfo(Util.user);
        }
        dialog = new ShareDialog(getActivity());
        dialog.setOnSelectClickListener(this);
    }

    /**
     * 更新用户信息
     *
     * @param user
     */
    private void userInfo(User user) {
//        Bitmap bitmap = BitmapFactory.decodeFile(Util.IMAGE_URL + user.getImgurl());
//        Bitmap bitmap1 = FastBlurUtil.doBlur(bitmap, 0, true);
//        imageView.setImageBitmap(bitmap1);
//        Glide.with(this).load(PICTURE_URL + user.getImgurl()).
//                diskCacheStrategy(DiskCacheStrategy.NONE)
//                .skipMemoryCache(true)
//                .error(R.mipmap.icon_head)
//                .placeholder(R.mipmap.icon_head)
//                .dontAnimate().into(imageView);

        Util.doBlur(imageView);

        Glide.with(this).load(PICTURE_URL + user.getImgurl()).
                diskCacheStrategy(DiskCacheStrategy.NONE)
                .skipMemoryCache(true)
                .error(R.mipmap.icon_head)
                .placeholder(R.mipmap.icon_head)
                .dontAnimate().into(ivHeader);
        tvUserName.setText(user.getNickname());
        if (user.getUserType() == 0) {
            tvJob.setText("身份: 艺人");
        } else {
            tvJob.setText("身份: 商家");
        }
        tvAccount.setText("账号:" + user.getTelephone());
    }

    @Override
    public void onResume() {
        super.onResume();
        userRestUsage.myInfo(myInfo, Util.UserId);
    }

    @OnClick({R.id.ibtnFocus, R.id.ibtnFans, R.id.ibtnChit, R.id.ibtnBag, R.id.tvMyHome, R.id.tvResume, R.id.tvPriceBag, R.id.tvAuthenticaiton,
            R.id.tvQuestion, R.id.tvSetting, R.id.tvSuggestion, R.id.tvFocus, R.id.tvFans, R.id.tvCircular, R.id.ll_title, R.id.tvShare})
    public void myOnClick(View view) {
        switch (view.getId()) {
            case R.id.ibtnFocus://关注
                startActivity(new Intent(getActivity(), MyFocusActivity.class));
                break;
            case R.id.ibtnFans://粉丝
                startActivity(new Intent(getActivity(), MyFansActivity.class));
                break;
            case R.id.ibtnChit://代金券
                break;
            case R.id.ibtnBag://钱包
                startActivity(new Intent(getActivity(), MyBagActivity.class));
                break;
            case R.id.tvMyHome://我的主页
                if (Util.JOB == 0) {
                    if ("1".equals(Util.ALENT)) {
                        startActivity(new Intent(getActivity(), MyArtistActivity.class
                        ));
                    } else ToastUtil.showShort(getActivity(), "请先加入人才库");

                } else {
                    startActivity(new Intent(getActivity(), BusinessHomepagerActivity.class));
                }
                break;
            case R.id.tvFocus://关注
                startActivity(new Intent(getActivity(), MyFocusActivity.class));
                break;
            case R.id.tvFans://粉丝
                startActivity(new Intent(getActivity(), MyFansActivity.class));
                break;
            case R.id.tvSuggestion://意见反馈
                startActivity(new Intent(getActivity(), SuggestionActivity.class));
                break;
            case R.id.tvQuestion://常见问题
                startActivity(new Intent(getActivity(), QuestionActivity.class));
                break;
            case R.id.tvSetting://设置
                startActivity(new Intent(getActivity(), SettingActivity.class));
                break;
            case R.id.tvCircular://我的通告
                startActivity(new Intent(getActivity(), MyNoticeActivity.class));
                break;
            case R.id.tvResume://我的简历
                startActivity(new Intent(getActivity(), MyResumeActivity.class));
                break;
            case R.id.ll_title://个人资料
                startActivity(new Intent(getActivity(), PersonalInfoActivity.class));
                break;
            case R.id.tvShare://分享
                dialog.show();
                break;
            case R.id.tvPriceBag://我的钱包
                ToastUtil.showShort(getActivity(), "相关功能开发中,敬请期待");
//                startActivity(new Intent(getActivity(), RechargeActivity.class));
                break;
            case R.id.tvAuthenticaiton://认证信息
                if (Util.JOB == 0)
                    startActivity(new Intent(getActivity(), PersonalAuthenticationActivity.class));
                else startActivity(new Intent(getActivity(), BusinessAuthenticationActivity.class));
                break;
        }
    }

    /**
     * 获得状态栏的高度
     *
     * @param context
     * @return
     */
    public int getStatusHeight(Context context) {

        int statusHeight = -1;
        try {
            Class<?> clazz = Class.forName("com.android.internal.R$dimen");
            Object object = clazz.newInstance();
            int height = Integer.parseInt(clazz.getField("status_bar_height")
                    .get(object).toString());
            statusHeight = context.getResources().getDimensionPixelSize(height);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return statusHeight;
    }


    @Override
    public void refreshUI(int taskId, MSG msg) {
        super.refreshUI(taskId, msg);
        if (taskId == myInfo) {
            if (msg.getData()) {
                ArtistsDetailedBean resultBean = (ArtistsDetailedBean) msg.getObj();
                User user = resultBean.getUser();
                if (user != null)
                    Util.user = user;
                userInfo(Util.user);
            }
        }
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.fragment_my;
    }

    @Override
    public void onOk(int shareType) {
        switch (shareType) {
            case 0:
                break;
            case 1:
                showWeChatShare(Wechat.NAME);
                break;
            case 2:
                showWeChatShare(WechatMoments.NAME);
                break;
            case 3:
                showQQShare(QQ.NAME);
                break;
            case 4:
                showQQShare(QZone.NAME);
                break;
            case 5:
                ToastUtil.showShort(getActivity(), "相关功能开发中,敬请期待");
                break;
        }
    }

    @Override
    public void onCancel() {

    }


    private void showWeChatShare(String type) {
        Platform.ShareParams params = new Platform.ShareParams();
//        Bitmap log = BitmapFactory.decodeResource(getResources(), R.mipmap.erweima);
        params.setShareType(Platform.SHARE_IMAGE);
//        params.setImageData(log);
        params.setImageUrl("http://www.pri.wutaibao.com/file/install_package_download.png");
        params.setTitle("舞台宝");
        Platform wechat = ShareSDK.getPlatform(type);
        wechat.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {

            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                ToastUtil.showShort(getActivity(), throwable.toString());
            }

            @Override
            public void onCancel(Platform platform, int i) {

            }
        });
        wechat.share(params);
    }

    private void showQQShare(String type) {
        Platform.ShareParams shareParams = new Platform.ShareParams();
//                shareParams.setTitle("测试分享的标题");
//                shareParams.setTitleUrl("http://sharesdk.cn");// 标题的超链接
//                shareParams.setText("测试分享的文本");
        shareParams.setImageUrl("http://www.pri.wutaibao.com/file/install_package_download.png");
        shareParams.setSite("舞台宝");
//                sp.setSiteUrl("发布分享网站的地址");

        Platform qzone = ShareSDK.getPlatform(type);

// 设置分享事件回调（注：回调放在不能保证在主线程调用，不可以在里面直接处理UI操作）
        qzone.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                //分享成功的回调
            }

            public void onError(Platform arg0, int arg1, Throwable arg2) {
                //失败的回调，arg:平台对象，arg1:表示当前的动作，arg2:异常信息
                ToastUtil.showShort(getActivity(), arg2.toString());
            }

            public void onCancel(Platform arg0, int arg1) {
                //取消分享的回调
            }
        });

// 执行图文分享
        qzone.share(shareParams);
    }
}
